package com.kevin.sql.wbuilder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;

public class WhereBuilder {
	@SuppressWarnings("serial")
	private static Map<String,String> patternMap = new HashMap<String,String>(){{
		put("like","like");
		put("likeor","likeor");
		put("between","between");
		put("equal","=");
		put("greater",">");
		put("greaterequal",">=");
		put("less","<");
		put("lessEqual","<=");
		put("in","in");
        put("notequal","<>");
        put("isnull","is null");
        put("notnull","is not null");
        put("and","and");
        put("or","or");
	}};
    public static String format(String searchList)
    {
        String sql = StringUtils.EMPTY;
    	try {
            List<WherePattern> sos = JSON.parseArray(searchList, WherePattern.class);
            if (sos != null && sos.size() > 0)
            {
                sql = buildSQL(sos);
            }
            return sql;
		} catch (Exception e) {
			return searchList;
		}      
    }
	
	private static String buildSQL(List<WherePattern> patterns)
    {
        StringBuilder sql = new StringBuilder();
        String key1 = " %s %s", key2 = " %s %s %s %s";
        if (patterns.size() > 1)//条件加括号。
        {
            key1 = " %s (%s)";
            key2 = " %s (%s %s %s)";
        }
        for (int i = 0; i < patterns.size(); i++)
        {
        	WherePattern obj = patterns.get(i);
            if (i == 0) { obj.setOrand(""); }
            else
            {
                obj.setOrand(obj.getOrand().equals("or")? "or" : "and");
            }
            String pattern = getOperate(obj.getPattern());
            switch (pattern)
            {
                case "in":
                    obj.setValue("(" + StringUtils.strip(obj.getValue(), ")(")+ ")");;
                    break;
                case "between":
                case ">":
                case ">=":
                case "<":
                case "<=":
                case "likeor":
                    break;
                case "is null":
                case "is not null":
                    obj.setValue("");
                    break;
                case "like":
                    obj.setValue("'%" + StringUtils.strip(obj.getValue(),"\'")+ "%'");
                    break;
                case "=":
                default:
                    obj.setValue("'" + StringUtils.strip(obj.getValue(),"\'") + "'");
                    break;
            }
            if (obj.getPattern().toLowerCase().equals("likeor"))//自定义sql
            {
                sql.append(String.format(key1, obj.getOrand(), obj.getValue()));
            }
            else
            {
                sql.append(String.format(key2, obj.getOrand(), obj.getName(), pattern, obj.getValue()));
            }
        }
        return sql.toString();
    }
	
    private static String getOperate(String key)
    {
        if (patternMap.containsKey(key))
        {
            return patternMap.get(key);
        }
        else
        {
            switch (key)
            {
                case ">":
                case ">=":
                case "<":
                case "<=":
                case "<>":
                    return key;
            }
            return "=";
        }
    }
}
